<?php
include_once dirname(__FILE__).'/autentificare.php';

function mysql_safer(){
 foreach ($_POST as $key => $value) {
     if(!is_array($_POST[$key])){
        $_POST[$key] = stripslashes($value); 
        $_POST[$key] = mysql_real_escape_string($value); }
        else{
            foreach ($_POST[$key] as $num => $value) {
                $_POST[$key][$num] = stripslashes($value); 
                $_POST[$key][$num] = mysql_real_escape_string($value);
            }
            
        }
  } 

  //This stops SQL Injection in GET vars 
  foreach ($_GET as $key => $value) { 
    $_GET[$key] = stripslashes($value); 
    $_GET[$key] = mysql_real_escape_string($value); 
  } 
}
class stdb{
    public $db_user;
    public $db_password;
    public $db_name;
    public $db_host;
    public $db_con;
    public $last_q;
    public $err;
    public $cmds=array();
   
    public function stdb(){
                                    global $db_user,$db_name,$db_password,$db_host;
                                    $this->db_host=$db_host;
                                    $this->db_user=$db_user;
                                    $this->db_password=$db_password;
                                    $this->db_name=$db_name;
                                    $this->db_con=null;
           
    }
    
    public function connect(){
       $this->db_con = mysql_connect($this->db_host,$this->db_user,$this->db_password);
        if (!$this->db_con)
                    {
                        die('Could not connect: ' . mysql_error());
                        }

        mysql_select_db($this->db_name,$this->db_con); 
        
        }
    
    
    public function disconnect(){
        mysql_close($this->db_con);
        unset($this->db_con);
    }
    
    public function q($query){
        $this->connect();
        $query = stripslashes($query);
        $result = mysql_query($query); 
        $this->last_q=$query;
        $this->err=mysql_errno($this->db_con) . ": " . mysql_error($this->db_con) . "\n";
        $this->disconnect();
        return $result;         
    }
    
    public function add_cmd($cmd){
         $this->cmds[]=$cmd;
    }
    public function commit()  //merge doar daca inainte de a declara vectorul de inserturi
    {                                           //se da comanda "mysql_query("begin");
        $ok = 1;
        $n=count($this->cmds);
        for($i=0;$i<$n;$i++)
        {
            $rezultat = $this->q($this->cmds[$i]);
            if( !$rezultat )
                $ok=0;
        }
        if( $ok == 1 )
        {
            $this->cmds("commit");
            return 1;
        }
        else
        {
            $this->cmds("rollback");  
            return 0;
        }
                  
    }
    
    public function debug(){
                $query=$this->last_q;
                 $this->connect();
                 mysql_query($query);
                echo mysql_errno($this->db_con) . ": " . mysql_error($this->db_con) . "\n";
                $this->disconnect();
//        if(is_resource($result))
//                    while($row = mysql_fetch_array($result))
//                        {
//                            //echo $row['username'] . " " . $row['password']. " " . $row['status'];
//                             print_r($row);
//                            echo "<br />";
//                        }  
    
    
        }
}   
   
?>
